import { formatTopSongs } from '../utils/song'
import axios from '../utils/axios'

// 获取歌单详情 默认值3778678
export function getPlayListDetail(id) {
  return new Promise((resolve, reject) => {
    axios.get('/playlist/detail', { params: { id } })
      .then(({ playlist }) => playlist || {})
      .then(playlist => {
        const { trackIds, tracks } = playlist
        if (!Array.isArray(trackIds) || tracks.length !== trackIds.length) {
          reject(new Error('获取歌单详情失败'))
          return
        }

        if(tracks.length === trackIds.length) {
          playlist.tracks = formatTopSongs(playlist.tracks)
          resolve(playlist)
          return 
        }
      })
  })
}

export function getMusicUrl(id) {
  return new Promise((resolve) => {
    axios.get('/song/url', { params: { id } })
      .then(res => {
        resolve(res)
        return
      })
  })
}